package com.jsz.peini.controller.app;

import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.jsz.peini.bean.user.UserAccount;
import com.jsz.peini.bean.user.UserImage;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.bean.user.UserLogin;
import com.jsz.peini.common.bean.ResultMessage;
import com.jsz.peini.common.bean.SmsVo;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.AppSecretUtil;
import com.jsz.peini.common.util.CommonUtils;
import com.jsz.peini.common.util.FileUtils;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.common.util.ThreadUtil;
import com.jsz.peini.mapper.app.AppActivityUserMapper;
import com.jsz.peini.mapper.comm.ImageFilterResultMapper;
import com.jsz.peini.mapper.user.UserAccountMapper;
import com.jsz.peini.mapper.user.UserImageMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.mapper.user.UserLoginMapper;
import com.jsz.peini.mapper.user.UserSmsMapper;
import com.jsz.peini.redis.base.RedisConstants;
import com.jsz.peini.redis.base.RedisUtil;
import com.jsz.peini.redis.base.SerializeUtil;
import com.jsz.peini.redis.util.UserRedisUtil;
import com.jsz.peini.server.thread.UserSquareThread;
import com.jsz.peini.service.sms.SmsService;
import com.jsz.peini.service.user.UserAccountService;
import com.jsz.peini.service.user.UserImageService;
import com.jsz.peini.service.user.UserInfoService;
import com.jsz.peini.service.user.UserLoginService;
import com.jsz.peini.service.user.UserTokenService;
import com.jsz.peini.vo.ImgVo;
import com.jsz.peini.vo.UserConcerVo;
import com.jsz.peini.vo.UserSearchVo;
import com.jsz.peini.vo.UserVo;

@Controller
public class UserController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserLoginService userLoginService;

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private UserImageService userImageService;

	@Autowired
	private UserTokenService userTokenService;

	@Autowired
	private UserAccountService userAccountService;

	@Autowired
	private SmsService smsService;
	@Resource
	private UserInfoMapper userInfoMapper;
	@Resource
	private UserAccountMapper userAccountMapper;
	@Resource
	private UserLoginMapper userLoginMapper;
	@Resource
	private UserSmsMapper userSmsMapper;
	@Resource
	private ImageFilterResultMapper imageFilterResultMapper;
	@Resource
	private UserImageMapper userImageMapper;
	@Resource
	private AppActivityUserMapper appActivityUserMapper;

	/**
	 * 用户登录
	 * @param userLogin
	 * @author zq
	 */
	@RequestMapping(value = "/userLogin")
	@ResponseBody
	public Object userLogin(HttpServletRequest request, /*UserLogin userLogin,*/
			@RequestParam(value="userName",required=false) String userName,
			@RequestParam(value="userPassword",required=false) String userPassword,
			@RequestParam(value="appA",required=false) String appA  //交换参数
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		// 用户名密码不能为空
		try {
			if(appA == null){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			if (StringUtils.isBlank(userName) || StringUtils.isBlank(userPassword)) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "用户名或密码不能为空");
				return infoMap;
			}
			
			if(userPassword.length() != 32){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			// 密码加密
			//userLogin.setUserPassword(MDUtil.encodePwd(userLogin.getUserPassword()));		
			// 检查账号密码是否存在
			
			List<UserLogin> loginList = userLoginMapper.selectByUserName(userName);
			if(loginList.size() == 0){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您输入的手机号尚未注册");
				return infoMap;
			}else{
				UserLogin userLoginBean = loginList.get(0);
				if(StringUtils.isBlank(userLoginBean.getUserPassword())){
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "您输入的密码不正确");
					return infoMap;
				}
				if(userLoginBean.getUserPassword().equals(userPassword)){
					if(userLoginBean.getAccstatus() != null && userLoginBean.getAccstatus() == 0){
						infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "该帐号已停用");
						return infoMap;
					}
					
					UserInfo userInfo = userInfoService.selectByUserLoginId(userLoginBean.getId());
					
					LinkedHashMap<String, Object> userMap = new LinkedHashMap<String, Object>();
					
					if(!PeiniUtils.isAddUserInfo(userInfo)){
						infoMap.put("addUserInfo", "0");
					}else{
						infoMap.put("addUserInfo", "1");
					}
					
					userInfo.setActiveTime(new Date());
					userInfoMapper.updateByPrimaryKeySelective(userInfo);
					
					// 登录成功生成新的token
					String userToken = UUID.randomUUID().toString().replace("-", "");
					userMap.put("id", userToken);
					
					userMap.put("userLoginId", userInfo.getUserLoginId());
					userMap.put("nickname", StringUtils.isNotBlank(userInfo.getNickname()) ? PeiniUtils.updateNickName(userInfo.getNickname(),userInfo.getAccCode()) : "");
					userMap.put("imageHead",
							StringUtils.isNotBlank(userInfo.getImageHead()) ? userInfo.getImageHead() : "");
					userMap.put("sex", userInfo.getSex());
					userMap.put("userPhone", userInfo.getUserPhone());
					
					UserRedisUtil.updateUserToken(userInfo.getId(), userToken);
					
					// 更新登录时间
					userLoginService.updateByUserLoginId(userLoginBean.getId());
					
					String serverB = AppSecretUtil.getAppSecret(userInfo.getId().toString(), appA);
					infoMap.put("serverB", serverB);
					
					infoMap.put("userToken", userToken);
					infoMap.put("userInfo", userMap);
				}else{
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "您输入的密码不正确");
					return infoMap;
				}
			}
			
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "登录成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		//logger.info(net.sf.json.JSONObject.fromObject(infoMap));
		return infoMap;
	}

	/**
	 * 帐号注册接口（创建手机个人帐号，创建个人信息）
	 * 
	 * @param userLogin
	 * @author jgx
	 */
	@RequestMapping(value = "/registerUserLogin")
	@ResponseBody
	public Object registerUserLogin(HttpServletRequest request,
			@RequestParam(value = "sex") Integer sex,
			@RequestParam(value = "birthday") String birthday,
			@RequestParam(value = "nowProvince") Integer nowProvince,
			@RequestParam(value = "nowCity") Integer nowCity,
			@RequestParam(value = "age") Integer age,
			@RequestParam(value = "userName") String userName,
			@RequestParam(value = "userPassword") String userPassword,
			@RequestParam(value="appA",required=false) String appA,  //交换参数
			@RequestParam(value="devNo",required=false,defaultValue="") String devNo,
			@RequestParam(value="channel",required=false,defaultValue="") String channel,
			@RequestParam(value="devtype",required=false) Integer devtype
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			if(appA == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			if(StringUtils.isBlank(userName) || StringUtils.isBlank(userPassword) || sex == null || StringUtils.isBlank(birthday) || nowProvince == null || nowCity == null || age == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			if(userPassword.length() != 32){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			//logger.info(request.getSession().getAttribute("devNo").toString());
			
			infoMap = userInfoService.register(request, sex, birthday, nowProvince, nowCity, age, userName, userPassword, appA, devNo, channel, devtype);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	
	/**
	 * 04-21 新版手机注册
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/registerUserLoginNew")
	public Object registerUserLoginNew(HttpServletRequest request,
			@RequestParam(value = "yzm") String yzm,
			@RequestParam(value = "userName") String userName,
			@RequestParam(value = "userPassword") String userPassword,
			@RequestParam(value="recId",required=false) String recId,
			@RequestParam(value = "thirdType",required=false) Integer thirdType,////三方类型：1QQ,2微信；3新浪微博
			@RequestParam(value = "thirdName",required=false) String thirdName,
			@RequestParam(value="appA",required=false) String appA,  //交换参数
			@RequestParam(value="devNo",required=false,defaultValue="") String devNo,
			@RequestParam(value="channel",required=false,defaultValue="") String channel,
			@RequestParam(value="devtype",required=false) Integer devtype
			){
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			if(appA == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			if(StringUtils.isBlank(userName) || StringUtils.isBlank(userPassword)){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			if(userPassword.length() != 32){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			SmsVo smsVo = UserRedisUtil.getSmsVo(userName);
			if (smsVo == null) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您输入的手机验证码不正确");
				return infoMap;
			} else {
				String yzmCode = smsVo.getYzm();
				Date sendTime = smsVo.getSendTime();
				
				// 验证码通过则清除验证码
				if (yzmCode.equals(yzm)) {
					
					//判断是否超时
					Date now = new Date();
					if(now.getTime()-sendTime.getTime() > 6*60*1000){
						infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "验证码已超时请重新获取");
						return infoMap;
					}
					
					UserRedisUtil.delSmsVo(userName);
				} else {
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "您输入的手机验证码不正确");
					return infoMap;
				}
			}
			
			infoMap = userInfoService.registerNew(request, userName, userPassword,thirdType,thirdName, appA, devNo, channel, devtype,recId);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 完善用户信息
	 * @author Han Guohong
	 * @param request
	 * @param userId
	 * @param sex
	 * @param birthday
	 * @param nowProvince
	 * @param nowCity
	 * @param age
	 * @param appA
	 * @param devNo
	 * @param devtype
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addUserInfo")
	public Object addUserInfo(HttpServletRequest request,
			@RequestParam(value = "userId") String userId,
			@RequestParam(value = "sex") Integer sex,
			@RequestParam(value = "birthday") String birthday,
			@RequestParam(value = "nowProvince") Integer nowProvince,
			@RequestParam(value = "nowCity") Integer nowCity,
			@RequestParam(value = "age") Integer age,
			@RequestParam(value="appA",required=false) String appA,  //交换参数
			@RequestParam(value="devNo",required=false,defaultValue="") String devNo,
			@RequestParam(value="devtype",required=false) Integer devtype
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			if(appA == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			if( sex == null || StringUtils.isBlank(birthday) || nowProvince == null || nowCity == null || age == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			infoMap = userInfoService.addUserInfo(request,userId, sex, birthday, nowProvince, nowCity, age, appA,devNo,devtype);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 新完善用户信息
	 * @author Han Guohong
	 * @param request
	 * @param userId
	 * @param sex
	 * @param birthday
	 * @param nowProvince
	 * @param nowCity
	 * @param age
	 * @param file
	 * @param appA
	 * @param devNo
	 * @param devtype
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addUserInfoNew")
	public Object addUserInfoNew(HttpServletRequest request,
			@RequestParam(value = "userId") String userId,
			@RequestParam(value = "sex") Integer sex,
			@RequestParam(value = "birthday") String birthday,
			@RequestParam(value = "nickName") String nickName,
			@RequestParam(value = "age") Integer age,
			@RequestParam("file") MultipartFile file,
			@RequestParam(value="appA",required=false) String appA,  //交换参数
			@RequestParam(value="devNo",required=false,defaultValue="") String devNo,
			@RequestParam(value="devtype",required=false) Integer devtype
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			if(appA == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			if( sex == null || StringUtils.isBlank(birthday) || StringUtils.isBlank(nickName) || age == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			infoMap = userInfoService.addUserInfoNew(request,userId, sex, birthday, nickName, age, appA,devNo,devtype,file);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 第三方注册	 * 
	 * @param userLogin
	 * @author jgx
	 */
	@ResponseBody
	@RequestMapping(value = "/registerThirdUserLogin")
	public Object registerThirdUserLogin(HttpServletRequest request,
			@RequestParam(value = "sex") Integer sex,
			@RequestParam(value = "birthday") String birthday,
			@RequestParam(value = "nowProvince") Integer nowProvince,
			@RequestParam(value = "nowCity") Integer nowCity,
			@RequestParam(value = "age") Integer age,
			@RequestParam(value = "userName") String userName,
			@RequestParam(value = "thirdType") Integer thirdType,////三方类型：1QQ,2微信；3新浪微博
			@RequestParam(value = "thirdName") String thirdName,
			@RequestParam(value = "nickname",required=false) String nickname,
			@RequestParam(value = "imageHead",required=false) MultipartFile imageHead,
			@RequestParam(value="appA",required=false) String appA,  //交换参数
			@RequestParam(value="devNo",required=false,defaultValue="") String devNo,
			@RequestParam(value="channel",required=false,defaultValue="") String channel,
			@RequestParam(value="devtype",required=false) Integer devtype
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			
			if(appA == null){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			if(StringUtils.isBlank(userName) || thirdType == null || sex == null || StringUtils.isBlank(birthday) || nowProvince == null || nowCity == null || age == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			infoMap = userInfoService.registerThirdUserLogin(request, sex, birthday, nowProvince, nowCity, age, userName, thirdType, thirdName, nickname, imageHead, appA, devNo, channel, devtype);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 新三方登录
	 * @author Han Guohong
	 * @param request
	 * @param yzm
	 * @param userName
	 * @param thirdType
	 * @param thirdName
	 * @param appA
	 * @param devNo
	 * @param channel
	 * @param devtype
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/registerThirdUserLoginNew")
	public Object registerThirdUserLoginNew(HttpServletRequest request,
			@RequestParam(value = "yzm") String yzm,
			@RequestParam(value = "userName") String userName,
			@RequestParam(value = "thirdType") Integer thirdType,////三方类型：1QQ,2微信；3新浪微博
			@RequestParam(value = "thirdName") String thirdName,
			@RequestParam(value="appA",required=false) String appA,  //交换参数
			@RequestParam(value="devNo",required=false,defaultValue="") String devNo,
			@RequestParam(value="channel",required=false,defaultValue="") String channel,
			@RequestParam(value="devtype",required=false) Integer devtype
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			
			if(appA == null){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			if(StringUtils.isBlank(userName) || thirdType == null || StringUtils.isBlank(yzm)){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			SmsVo smsVo = UserRedisUtil.getSmsVo(userName);
			if (smsVo == null) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您输入的手机验证码不正确");
				return infoMap;
			} else {
				String yzmCode = smsVo.getYzm();
				Date sendTime = smsVo.getSendTime();
				
				// 验证码通过则清除验证码
				if (yzmCode.equals(yzm)) {
					
					//判断是否超时
					Date now = new Date();
					if(now.getTime()-sendTime.getTime() > 6*60*1000){
						infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "验证码已超时请重新获取");
						return infoMap;
					}
					
					UserRedisUtil.delSmsVo(userName);
				} else {
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "您输入的手机验证码不正确");
					return infoMap;
				}
			}
			
			infoMap = userInfoService.registerThirdUserLoginNew(request, userName, thirdType,thirdName, appA, devNo, channel, devtype);
			
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 第三方用户登录
	 * 
	 * @param userLogin
	 * @author zq
	 */
	@RequestMapping(value = "/userLoginByThird")
	@ResponseBody
	public Object userLoginByThird(HttpServletRequest request, /*UserLogin userLogin,*/
			@RequestParam(value="thirdName") String thirdName,
			@RequestParam(value="appA",required=false) String appA  //交换参数
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		// 检查第三方用户是否存在
		try {
			if(appA == null){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			appA = AppSecretUtil.getAppA(appA);
			if(StringUtils.isBlank(appA)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			UserLogin userLogin = new UserLogin();
			userLogin.setThirdName(thirdName);
			
			UserLogin userLoginBean = userLoginService.selectByThirdName(userLogin);
			if (userLoginBean == null) {
				infoMap.put("isExist", Constants.FAIL);
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "用户不存在");
				return infoMap;
			} else {
				if(userLoginBean.getAccstatus() != null && userLoginBean.getAccstatus() == 0){
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "该帐号已停用");
					return infoMap;
				}
				
				UserInfo userInfo = userInfoService.selectByUserLoginId(userLoginBean.getId());
				
				if(!PeiniUtils.isAddUserInfo(userInfo)){
					infoMap.put("addUserInfo", "0");
				}else{
					infoMap.put("addUserInfo", "1");
				}
				
				userInfo.setActiveTime(new Date());
				userInfoMapper.updateByPrimaryKeySelective(userInfo);
				
				LinkedHashMap<String, Object> userMap = new LinkedHashMap<String, Object>();
				
				// 登录成功生成新的token
				String userToken = UUID.randomUUID().toString().replace("-", "");
				
				userMap.put("id", userToken);
				userMap.put("userLoginId", userInfo.getUserLoginId());
				userMap.put("nickname", StringUtils.isNotBlank(userInfo.getNickname()) ? PeiniUtils.updateNickName(userInfo.getNickname(),userInfo.getAccCode()) : "");
				userMap.put("imageHead",
						StringUtils.isNotBlank(userInfo.getImageHead()) ? userInfo.getImageHead() : "");
				userMap.put("sex", userInfo.getSex());
				userMap.put("phone", userLoginBean.getUserName());
				
				//EhcacheUtil.getInstance().put("userCache", userInfo.getId().toString(), userToken);
				UserRedisUtil.updateUserToken(userInfo.getId(), userToken);
				// 更新登录时间
				userLoginService.updateByUserLoginId(userLoginBean.getId());
				
				String serverB = AppSecretUtil.getAppSecret(userInfo.getId().toString(), appA);
				infoMap.put("serverB", serverB);
				
				infoMap.put("userToken", userToken);
				infoMap.put("userInfo", userMap);
				
			}
			infoMap.put("isExist", Constants.SUCCESS);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "登录成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 修改密码
	 * 
	 * @param userLogin
	 * @author zq
	 */
	@RequestMapping(value = "/updatePassword")
	@ResponseBody
	public Object updatePassword(HttpServletRequest request, UserLogin userLogin) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			if (StringUtils.isBlank(userLogin.getUserPassword())) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "密码不能为空");
				return infoMap;
			}
			
			if(userLogin.getUserPassword().length() != 32){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			UserLogin userLoginBean = new UserLogin();
			userLoginBean.setUserName(userLogin.getUserName());
			//userLoginBean.setUserPassword(MDUtil.encodePwd(userLogin.getUserPassword()));
			userLoginBean.setUserPassword(userLogin.getUserPassword());
			int updateNum = userLoginService.updatePwdByUserName(userLoginBean);
			if (updateNum > 0) {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "密码修改成功");
			} else {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "密码修改失败");
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 检查密码
	 * 
	 * @param userLogin
	 * @author zq
	 */
	@RequestMapping(value = "/checkPassword")
	@ResponseBody
	public Object checkPassword(HttpServletRequest request, UserLogin userLogin) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			if (StringUtils.isBlank(userLogin.getUserPassword())) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "密码不能为空");
				return infoMap;
			}
			
			if(userLogin.getUserPassword().length() != 32){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			// 密码加密
			//userLogin.setUserPassword(MDUtil.encodePwd(userLogin.getUserPassword()));
			// 检查账号密码是否存在
			UserLogin userLoginBean = userLoginService.selectByLogin(userLogin);
			if (userLoginBean == null) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "原始密码不正确");
				return infoMap;
			} else {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "密码正确");
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 手机号验证码验证 
	 * @param userLogin
	 * @author zq
	 */
	@RequestMapping(value = "/checkSmsCode")
	@ResponseBody
	public Object checkSmsCode(HttpServletRequest request,
			@RequestParam(value = "userName") String userName,
			String yzm) {
		
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		// 取验证码
		try {
			//Object yzmObject = EhcacheUtil.getInstance().get("smsCache", userName);
			SmsVo smsVo = UserRedisUtil.getSmsVo(userName);
			if (smsVo == null) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您输入的手机验证码不正确");
				return infoMap;
			} else {
				//SmsVo smsVo = (SmsVo)yzmObject;
				String yzmCode = smsVo.getYzm();
				Date sendTime = smsVo.getSendTime();
				
				// 验证码通过则清除验证码
				if (yzmCode.equals(yzm)) {
					
					//判断是否超时
					Date now = new Date();
					if(now.getTime()-sendTime.getTime() > 6*60*1000){
						infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "验证码已超时请重新获取");
						return infoMap;
					}
					
					//EhcacheUtil.getInstance().remove("smsCache", userName);
					UserRedisUtil.delSmsVo(userName);
				} else {
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "您输入的手机验证码不正确");
					return infoMap;
				}
			}
			infoMap.put("userLoginName", userName);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "验证码正确");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 手机号注册验证码发送接口
	 * 
	 * @param userLogin
	 * @author jgx
	 */
	@RequestMapping(value = "/smsSendRegister")
	@ResponseBody
	public Object smsSend(HttpServletRequest request,
			@RequestParam(value="userName") String userName
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			
			if(StringUtils.isBlank(userName)){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
			}
			
			UserLogin userLoginBean = userLoginService.selectByUserName(userName);
			if (userLoginBean == null) {
				// 生成验证码
				String yzm = CommonUtils.createRandom(true, 6);
				
				SmsVo smsVo = new SmsVo(yzm,new Date());
				
				// 发送验证码
				boolean smsResult = smsService.sendMessage(userName, yzm, Constants.SMS_YZM);
				// boolean smsResult = true;//测试用
				if (smsResult) {
					infoMap.put("resultCode", Constants.SUCCESS);
					infoMap.put("resultDesc", "已发送");
				} else {
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "发送失败");
				}
				//EhcacheUtil.getInstance().put("smsCache", userName, smsVo);
				UserRedisUtil.updateTelAuthcode(userName, smsVo);
				
				infoMap.put("userName", userName);
				infoMap.put("smsCode", yzm);// 测试用
			} else {
				infoMap.put("userName", userName);
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您输入的手机号已注册");
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
//		logger.info(net.sf.json.JSONObject.fromObject(infoMap));
		return infoMap;
	}

	/**
	 * 手机号找回密码验证码发送接口
	 * 
	 * @param userLogin
	 * @author zq
	 */
	@RequestMapping(value = "/smsSendFind")
	@ResponseBody
	public Object smsSendFind(HttpServletRequest request, UserLogin userLogin) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			UserLogin userLoginBean = userLoginService.selectByUserName(userLogin.getUserName());
			if (userLoginBean == null) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "该用户不存在,请先注册");
				return infoMap;
			} else {
				// 生成验证码
				String yzm = CommonUtils.createRandom(true, 6);
				
				SmsVo smsVo = new SmsVo(yzm,new Date());
				
				// 发送验证码
				boolean smsResult = smsService.sendMessage(userLogin.getUserName(), yzm,Constants.SMS_YZM);

				//boolean smsResult = true;// 测试用

				if (smsResult) {
					infoMap.put("resultCode", Constants.SUCCESS);
					infoMap.put("resultDesc", "已发送");
				} else {
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "发送失败");
				}
				//EhcacheUtil.getInstance().put("smsCache", userLogin.getUserName(), smsVo);
				UserRedisUtil.updateTelAuthcode(userLogin.getUserName(), smsVo);

				infoMap.put("smsCode", yzm);// 测试用

				infoMap.put("userName", userLogin.getUserName());
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 手机号支付密码验证码发送接口
	 * 
	 * @param userName
	 *            手机号
	 * @author zq
	 */
	@RequestMapping(value = "/smsSendPay")
	@ResponseBody
	public Object smsSendPay(HttpServletRequest request,
			@RequestParam(value="userToken") String userToken
			) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			JSONObject obj = PeiniUtils.validToken(userToken);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			
			UserLogin userLogin = userLoginService.selectByUserId(userId);
			
			// 生成验证码
			String yzm = CommonUtils.createRandom(true, 6);
			// 发送验证码
			boolean smsResult = smsService.sendMessage(userLogin.getUserName(), yzm, Constants.SMS_YZM);
			
			SmsVo smsVo = new SmsVo(yzm,new Date());
			
			if (smsResult) {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "已发送");
			} else {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "发送失败");
			}
			//EhcacheUtil.getInstance().put("smsCache", userLogin.getUserName(), smsVo);
			UserRedisUtil.updateTelAuthcode(userLogin.getUserName(), smsVo);
			
			infoMap.put("smsCode", yzm);
			infoMap.put("userName", userLogin.getUserName());
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 修改支付密码
	 * 
	 * @param userName
	 *            手机号码,password 密码,userId 用户id,yzm 验证码
	 * @author zq
	 */
	@RequestMapping(value = "/updatePayPassword")
	@ResponseBody
	public Object updatePayPassword(HttpServletRequest request, 
			@RequestParam(value="userPassword") String userPassword, 
			@RequestParam(value="userToken") String userToken,
			String yzm) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			if (StringUtils.isBlank(userPassword)) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "密码不能为空");
				return infoMap;
			}
			
			if(userPassword.length() != 32){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			JSONObject obj = PeiniUtils.validToken(userToken);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			
			UserLogin userLogin = userLoginService.selectByUserId(userId);
			
			//Object yzmObject = EhcacheUtil.getInstance().get("smsCache", userLogin.getUserName());
			SmsVo smsVo = UserRedisUtil.getSmsVo(userLogin.getUserName());
			if (smsVo == null) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您输入的手机验证码不正确");
				return infoMap;
			} else {
				//SmsVo smsVo = (SmsVo)yzmObject;
				String yzmCode = smsVo.getYzm();
				Date sendTime = smsVo.getSendTime();
				
				// 验证码通过则清除验证码
				if (yzmCode.equals(yzm)) {
					
					//判断是否超时
					Date now = new Date();
					if(now.getTime()-sendTime.getTime() > 5*60*1000){
						infoMap.put("resultCode", Constants.FAIL);
						infoMap.put("resultDesc", "验证码已超时请重新获取");
						return infoMap;
					}
					
					//EhcacheUtil.getInstance().remove("smsCache", userLogin.getUserName());
					UserRedisUtil.delSmsVo(userLogin.getUserName());
				} else {
					infoMap.put("resultCode", Constants.FAIL);
					infoMap.put("resultDesc", "您输入的手机验证码不正确");
					return infoMap;
				}
			}
			
			UserAccount userAccount = new UserAccount();
			userAccount.setUserId(userId);
			//userAccount.setPayPassword(MDUtil.encodePwd(userPassword));
			userAccount.setPayPassword(userPassword);
			int updateNum = userAccountService.updatePayPwd(userAccount);
			if (updateNum > 0) {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "密码设置成功");
			} else {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "密码设置失败");
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 照片上传
	 * 
	 * @param request
	 * @param file
	 * @param userInfoId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/setUserImages", method = RequestMethod.POST)
	@ResponseBody
	public Object setUserImages(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile[] file,
			String userInfoId) {
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		infoMap.put("resultCode", Constants.SUCCESS);
		infoMap.put("resultDesc", "成功");
		// 判断文件是否为空
		try {
			JSONObject obj = PeiniUtils.validToken(userInfoId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			
			Date now = new Date();
			/*String nowStr = DateTimeUtils.formatDateTime2ShortString(now);
			nowStr += " 00:00:00";
			now = DateTimeUtils.formatString2Date(nowStr, "yyyy-MM-dd HH:mm:ss");*/
			
			// 文件保存路径
			String filePath = "/upload/userimage/";
			for (MultipartFile fileMap : file) {
				// 判断文件是否为空
				if (!fileMap.isEmpty()) {
					String resultPath = FileUtils.uploadFile(fileMap, filePath);
					UserImage reportImage = new UserImage();
					reportImage.setImageSrc(resultPath);
					reportImage.setUserId(userId);
					reportImage.setImageTime(now);
					reportImage.setIsdelete(0);
					//userImageService.insertUserImageById(reportImage);
					userImageMapper.insertSelective(reportImage);
					
					ImgVo imgVo = new ImgVo();
					imgVo.setImgUrl(resultPath);
					imgVo.setParamId(reportImage.getId().toString());
					imgVo.setType(4);
					RedisUtil.lpush(RedisConstants.IMG_CHECK.getBytes(), SerializeUtil.serialize(imgVo));
					
					/*Integer result = ImageFilterUtil.checkImg(fileMap);
					if(result == 0 || result == 1){
						String resultPath = FileUtils.uploadFile(fileMap, filePath);
						UserImage reportImage = new UserImage();
						reportImage.setImageSrc(resultPath);
						reportImage.setUserId(userId);
						userImageService.insertUserImageById(reportImage);
						
						if(result == 1){
							ImageFilterResult image = new ImageFilterResult();
							image.setImgurl(resultPath);
							image.setUrltype("userImage");
							imageFilterResultMapper.insert(image);
						}
					}*/
				}
			}
			UserInfo newuserInfo = userInfoMapper.selectByPrimaryKey(userId);
			UserConcerVo concerVo = new UserConcerVo(newuserInfo.getId(), null, null,3, newuserInfo.getNickname(), newuserInfo.getAccCode(), newuserInfo.getSignWord());
			UserSquareThread.userInfo.add(concerVo);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
		}
		return infoMap;
	}

	/**
	 * 照片删除
	 * 
	 * @param request
	 * @param imageId
	 * @return
	 */
	@RequestMapping(value = "/deleteUserImage")
	@ResponseBody
	public Object deleteUserImage(HttpServletRequest request, String imageId) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {

			String[] imgArr = imageId.split(",");
			for (int i = 0; i < imgArr.length; i++) {
				// 查找照片路径
				if (StringUtil.isEmpty(imgArr[i])) {
					continue;
				}
				UserImage userImage = userImageService.selectUserImageByUserId(Integer.parseInt(imgArr[i]));
				// 删除照片
				if (userImage != null) {
					File file = new File(FileUtils.fileUrl + userImage.getImageSrc());
					//logger.info(file.getAbsolutePath());
					boolean filedelete = false;
					if (file.exists()) {
						filedelete = file.delete();
						logger.info("------------图片删除成功-------------");
					}
				}
				// 删除对应记录信息
				int userImageDelete = userImageService.deleteUserImageByUserId(Integer.parseInt(imgArr[i]));
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "成功删除"+userImageDelete+"张图片");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
		}
		return infoMap;
	}

	// /**
	// * 查看照片信息
	// * @param userId 用户id
	// * @throws IOException
	// */
	// @RequestMapping(value="/getUserImage")
	// public String getUserImage(HttpServletRequest request,HttpServletResponse
	// response,int imageId) throws IOException{
	// response.setContentType("image/png"); // 设置返回内容格式
	// String picurl = request.getSession().getServletContext().getRealPath("/")
	// + "img/noimage.png";//设置如果没有图片默认的显示
	// UserImage userInfo = userImageService.selectUserImageByUserId(imageId);
	// //如果头像信息不为空
	// if(StringUtils.isNotBlank(userInfo.getImageSrc())){
	// picurl = userInfo.getImageSrc();
	// }
	// File file = new File(picurl);
	// if(file.exists()){ //如果文件存在
	// InputStream in = new FileInputStream(picurl); //用该文件创建一个输入流
	// OutputStream os = response.getOutputStream(); //创建输出流
	// byte[] b = new byte[1024];
	// while( in.read(b)!= -1){
	// os.write(b);
	// }
	// in.close();
	// os.flush();
	// os.close();
	// }
	// return null;
	// }
	//
	/**
	 * 相册列表
	 * 
	 * @param request
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/getUserImageAllByUserId")
	@ResponseBody
	public Object getUserImageAllByUserId(HttpServletRequest request,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="page") Integer page,
			@RequestParam(value="rows") Integer rows,
			@RequestParam(value="type",defaultValue="1") Integer type) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			if(type == 1){
				JSONObject obj = PeiniUtils.validToken(userId);
				if(obj.getInteger("resultCode") != 1){
					
				}else{
					userId = obj.getString("userId");
				}
			}
			// 查找所有的图片
			//List<LinkedHashMap<String, Object>> userImageAll = userImageService.selectUserImageAllByUserId(userId);
			
			// 查找所有天数类
			PageHelper.startPage(page, rows);
			List<LinkedHashMap<String, Object>> userImageType = userImageService.selectUserImageTypeByUserId(userId);
			List<String> timeList = new ArrayList<String>();
			for (LinkedHashMap<String, Object> timeMap : userImageType) {
				timeList.add(timeMap.get("imageTime").toString());
			}
			
			// 新的用于返回的list
			List<LinkedHashMap<String, Object>> userImageList = new ArrayList<LinkedHashMap<String, Object>>();
			if(timeList.size()>0){
				HashMap<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("userId", userId);
				paramMap.put("imageTime", timeList);
				List<LinkedHashMap<String, Object>> userImageAll = userImageService.selectUserImageAllByUserId(userId);
				
				for (int i = 0; i < userImageType.size(); i++) {

					LinkedHashMap<String, Object> imageObj = new LinkedHashMap<String, Object>();
					//imageObj.put("imageTime", userImageType.get(i).get("imageTime"));
					imageObj.put("imageTime", userImageType.get(i).get("imageTime"));
					List<LinkedHashMap<String, Object>> imageList = new ArrayList<LinkedHashMap<String, Object>>();
					for (int j = 0; j < userImageAll.size(); j++) {
						if (userImageAll.get(j).get("imageTime").equals(userImageType.get(i).get("imageTime"))) {
							// userImageType.add(userImageAll.get(j));
							userImageAll.get(j).put("imageTime", userImageAll.get(j).get("imageTime"));
							imageList.add(userImageAll.get(j));
						}
					}
					imageObj.put("userImageAll", imageList);
					// 将天数类放入返回数据中
					userImageList.add(imageObj);

				}
			}
			
			infoMap.put("userImageList", userImageList);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			infoMap.put("resultCode", Constants.FAIL);
			infoMap.put("resultDesc", "网络请求失败");
		}
		return infoMap;
	}

	/**
	 * 更换绑定手机号
	 * 
	 * @param userLoginId
	 *            用户登录id,userName 用户手机号
	 * @author zq
	 */
	@RequestMapping(value = "/updateLoginName")
	@ResponseBody
	public Object updateLoginName(HttpServletRequest request,
			@RequestParam(value="userId") String userId,
			@RequestParam(value="userName") String userName) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
			/*UserVo userVo = new UserVo();
			userVo.setAcountId(String.valueOf(userAccount.getId()));
			userVo.setUserId(userInfo.getId());
			userVo.setUserLoginId(userLogin.getId().toString());
			userVo.setBuyList(new BigDecimal("0"));
			userVo.setGoldList(new BigDecimal("0"));
			userVo.setIntegrityList(new BigDecimal("0"));
			UserRedisUtil.addUserToRedis(userVo);*/
			
			UserVo userVo = UserRedisUtil.getUserVoByUserId(userId);
			if(userVo == null){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			userVo.setUserId(userInfo.getId());
			userVo.setUserLoginId(userInfo.getUserLoginId().toString());
			UserRedisUtil.addUserToRedis(userVo);
			
			String userLoginId = userVo.getUserLoginId();
			if(StringUtils.isBlank(userId)){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			
			UserLogin userLogin = userLoginMapper.selectByPrimaryKey(Integer.parseInt(userLoginId));
			if(userLogin.getUserName().equals(userName)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "不能与原手机号相同");
				return infoMap;
			}
			
			UserLogin existLogin = userLoginService.selectByUserName(userName);
			if (existLogin != null) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "您输入的手机号已与其他账户绑定");
				return infoMap;
			}
			
			//userLogin.setId(Integer.parseInt(userLoginId));
			userLogin.setUserName(userName);
			int updateNum = userLoginService.updatePhoneByUserLoginId(userLogin);
			
			if (updateNum > 0) {
				
				userInfo.setUserPhone(userName);
				userInfoMapper.updateByPrimaryKey(userInfo);
				
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "新手机绑定成功");
			} else {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "新手机绑定失败");
			}
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}

	/**
	 * 身份验证失效
	 * 
	 * @return
	 */
	@RequestMapping(value = "/back/wrongToken")
	@ResponseBody
	public Object wrongToken(HttpServletRequest request) {
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		infoMap.put("resultCode", Constants.INVALID);
		infoMap.put("resultDesc", "身份失效");
		return infoMap;
	}

	@RequestMapping(value = "/resetPass")
	@ResponseBody
	public Object resetPass(HttpServletRequest request, 
			@RequestParam(value="userToken") String userToken,
			@RequestParam(value="oldPass") String oldPass,
			@RequestParam(value="newPass") String newPass
			) {
		
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			JSONObject obj = PeiniUtils.validToken(userToken);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			
			if (StringUtils.isBlank(oldPass) || StringUtils.isBlank(newPass)) {
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "密码不能为空");
				return infoMap;
			}
			
			if(oldPass.length() != 32 || newPass.length() != 32){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "无效请求");
				return infoMap;
			}
			
			UserLogin login = userLoginService.selectByUserId(userId);
			//if(!login.getUserPassword().equals(MDUtil.encodePwd(oldPass))){
			if(!login.getUserPassword().equals(oldPass)){
				infoMap.put("resultCode", Constants.FAIL);
				infoMap.put("resultDesc", "原密码不正确");
				return infoMap;
			}
			
			//login.setUserPassword(MDUtil.encodePwd(newPass));
			login.setUserPassword(newPass);
			userLoginService.updateByPrimaryKeySelective(login);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "密码修改成功");
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	/**
	 * 保存app切换到后台时间
	 * @param request
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/saveLastActiveTime")
	@ResponseBody
	public Object saveLastActiveTime(HttpServletRequest request, 
			@RequestParam(value="userId") String userId
			) {
		
		HashMap<String, Object> infoMap = new HashMap<String, Object>();
		try {
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			userId = obj.getString("userId");
			
			UserInfo info = new UserInfo();
			info.setId(userId);
			info.setActiveTime(new Date());
			userInfoMapper.updateByPrimaryKeySelective(info);
			
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", Constants.SUCCESS_DESC);
		} catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
	}
	
	private static String[] userFields = new String[]{"id","nickname","imageHead","sex","age","constellation","industry","smallIncome","bigIncome","activeTime","activeTimeStr","distance","distanceStr","zmPoint","credit"};
	@ApiOperation("用户筛选")
	@RequestMapping(value = "/user/search", method=RequestMethod.POST, consumes="application/json")
	@ResponseBody
	public Object searchUser(HttpServletRequest request, @RequestBody @Valid UserSearchVo vo, BindingResult binding) {
		if(ThreadUtil.getThreadInfo().getUserId() == null){
			return ResultMessage.AUTH_FAILED;
		}
		if(binding.hasErrors()){
			return ResultMessage.FAIL.setBindingResult(binding);
		}
		vo.setXpoint(ThreadUtil.getThreadInfo().getXpoint());
		vo.setYpoint(ThreadUtil.getThreadInfo().getYpoint());
		if(vo.getXpoint() == null || vo.getYpoint() == null){
			return ResultMessage.FAIL.setMessage("未获取到您的定位");
		}
		List<UserInfo> userList = userInfoService.searchUserInfo(vo);
		return ResultMessage.SUCCESS.setData(userList,userFields);
	}
}
